<?php namespace Summer\Hub;

use Backend\Classes\ListColumn;
use Lang;
use Model;

class ListButtonField
{

    /**
     * @param       $field
     * @param array $config
     * @internal param $name
     */


    public static function render($value, ListColumn $column, Model $record)
    {
        $field = new self($value, $column, $record);
        $buttons = $field->getButtons();

        return $buttons;
    }

    /**
     * ListSwitchField constructor.
     *
     * @param            $value
     * @param ListColumn $column
     * @param Model      $record
     */
    public function __construct($value, ListColumn $column, Model $record)
    {
        $this->name = $column->columnName;
        $this->value = $value;
        $this->column = $column;
        $this->record = $record;
    }

    /**
     * 获取要显示的按钮
     */
    private function getButtons()
    {
        $buttons=$this->column->config['buttons'];
        if(!empty($buttons)){
            $btn_html='';
            foreach ($buttons as $bv){

                //判断是否显示
                $btnShowField=$bv['btn']["btnShowField"]??"";
                if($btnShowField){
                    if($this->record->$btnShowField!=$bv['btn']["btnShowValue"]){
                        continue;
                    }
                }
                //按钮类型 confirm-询问层 prompt-prompt层
                $btnType=$bv['btn']["btnType"]??"";
                if($btnType=="confirm"){
                    $btn_html.=$this->getBtnConfirm($bv['btn']);
                }
                if($btnType=="prompt"){
                    $btn_html.=$this->getBtnPrompt($bv['btn']);
                }
                if($btnType=="info"){
                    $btn_html.=$this->getBtnInfo($bv['btn']);
                }
                if($btnType==""){
                    $btn_html.=$this->getBtnDefault($bv['btn']);
                }

            }
            return $btn_html;
        }
    }

    /**
     * 默认按钮
     * @param $btn
     * @return string
     */
    private function getBtnPrompt($btn)
    {
        //按钮标题
        $btnTitle=$btn["btnTitle"];
        //按钮样式 包括 类型、icon、大小
        $btnClass=$btn["btnClass"]??"btn-default";
        //询问后执行的操作
        $btnRequestParams='';
        $btnRequest=$btn["btnRequest"]??"";
        if($btnRequest){
            $btnRequestParams=",data_request:'".$btnRequest."'";
        }
        //询问的title
        $btnPromptTitleParams='';
        $btnPromptTitle=$btn["btnTypeTitle"]??"";
        if($btnPromptTitle){
            $btnPromptTitleParams=",prompt_title:'".$btnPromptTitle."'";
        }
        //询问的字段配置文件路径
        $btnConfigPathParams='';
        $btnConfigPath=$btn["btnConfigPath"]??"";
        if($btnConfigPath){
            $btnConfigPathParams=",config_path:'".$btnConfigPath."'";
        }

        $btn_html='
                <button type="button" class="btn '.$btnClass.'" 
                data-control="popup"
                data-handler="onListBtnPromptForm"
                data-request-data="record_id:'.$this->record->id.$btnRequestParams.$btnPromptTitleParams.$btnConfigPathParams.'"
                >
                '.$btnTitle.'
                </button>
                ';
        return $btn_html;
    }

    /**
     * 询问层按钮
     * @param $btn
     * @return string
     */
    private function getBtnConfirm($btn)
    {
        //按钮标题
        $btnTitle=$btn["btnTitle"];
        //按钮样式 包括 类型、icon、大小
        $btnClass=$btn["btnClass"]??"btn-default";
        //自定义询问层标题 注：询问层需配合data-request使用
        $btnConfirmTitle=$btn["btnTypeTitle"]??"您确定执行该操作？";
        $btnConfirmHtml='data-request-confirm="'.$btnConfirmTitle.'"';
        //AJAX操作 传递的数据和列表批量操作结构一致
        $btnRequestHtml='';
        $btnRequest=$btn["btnRequest"]??"";
        if($btnRequest){
            $btnRequestHtml='data-request="'.$btnRequest.'"';
        }
        $btn_html='
                <button type="button" class="btn '.$btnClass.'"
                '.$btnRequestHtml.'
                '.$btnConfirmHtml.'
                data-request-data="checked[]:'.$this->record->id.'"
                >
                '.$btnTitle.'
                </button>
                ';
        return $btn_html;
    }
    /**
     * 默认按钮
     * @param $btn
     * @return string
     */
    private function getBtnDefault($btn)
    {
        //按钮标题
        $btnTitle=$btn["btnTitle"];
        //按钮样式 包括 类型、icon、大小
        $btnClass=$btn["btnClass"]??"btn-default";
        $btn_html='
                <button type="button" class="btn '.$btnClass.'" >
                '.$btnTitle.'
                </button>
                ';
        return $btn_html;
    }

    /**
     * 弹框展示信息
     * @param $btn
     * @return string
     */
    private function getBtnInfo($btn)
    {
        //按钮标题
        $btnTitle=$btn["btnTitle"];
        //按钮样式 包括 类型、icon、大小
        $btnClass=$btn["btnClass"]??"btn-default";
        //询问后执行的操作
        $btnRequestParams='';
        $btnRequest=$btn["btnRequest"]??"";
        if($btnRequest){
            $btnRequestParams=",data_request:'".$btnRequest."'";
        }
        //询问的title
        $btnPromptTitleParams='';
        $btnPromptTitle=$btn["btnTypeTitle"]??"";
        if($btnPromptTitle){
            $btnPromptTitleParams=",prompt_title:'".$btnPromptTitle."'";
        }
        //询问的字段配置文件路径
        $btnConfigPathParams='';
        $btnConfigPath=$btn["btnConfigPath"]??"";
        if($btnConfigPath){
            $btnConfigPathParams=",config_path:'".$btnConfigPath."'";
        }

        $btn_html='
                <button type="button" class="btn '.$btnClass.'" 
                data-control="popup"
                data-handler="onListBtnInfoForm"
                data-request-data="record_id:'.$this->record->id.$btnRequestParams.$btnPromptTitleParams.$btnConfigPathParams.'"
                >
                '.$btnTitle.'
                </button>
                ';
        return $btn_html;
    }


}